from flask import Blueprint, render_template, request, jsonify
import requests
from flask_login import login_required, current_user

# 创建蓝图
music_bp = Blueprint('music', __name__, url_prefix='/music')

# 音乐搜索页面
@music_bp.route('/')
@login_required
def index():
    return render_template('music.html', username=current_user.username)

# 搜索音乐API
@music_bp.route('/search', methods=['GET'])
@login_required
def search():
    key = request.args.get('key', '')
    page = request.args.get('pn', '1')
    
    if not key:
        return jsonify([])
    
    try:
        # 构建API请求URL
        api_url = f'https://musicbox-web-api.mu-jie.cc/wyy/search?key={key}&pn={page}'
        
        # 设置请求头
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
        }
        
        # 发送请求
        response = requests.get(api_url, headers=headers, timeout=10)
        response.raise_for_status()
        
        # 返回JSON响应
        return response.text
    except requests.exceptions.RequestException as e:
        return jsonify({'error': f'API请求失败: {str(e)}'}), 500
    except Exception as e:
        return jsonify({'error': f'处理请求时发生错误: {str(e)}'}), 500

# 获取MP3链接API
@music_bp.route('/mp3', methods=['GET'])
@login_required
def get_mp3():
    rid = request.args.get('rid', '')
    
    if not rid:
        return jsonify({'error': '未提供音乐ID'}), 400
    
    try:
        # 构建API请求URL
        api_url = f'https://musicbox-web-api.mu-jie.cc/wyy/mp3?rid={rid}'
        
        # 设置请求头
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
        }
        
        # 发送请求
        response = requests.get(api_url, headers=headers, timeout=10)
        response.raise_for_status()
        
        # 返回MP3链接
        return response.text
    except requests.exceptions.RequestException as e:
        return jsonify({'error': f'API请求失败: {str(e)}'}), 500
    except Exception as e:
        return jsonify({'error': f'处理请求时发生错误: {str(e)}'}), 500

# 获取歌词API
@music_bp.route('/lyric', methods=['GET'])
@login_required
def get_lyric():
    rid = request.args.get('rid', '')
    
    if not rid:
        return jsonify({'error': '未提供音乐ID'}), 400
    
    try:
        # 构建API请求URL
        api_url = f'https://musicbox-web-api.mu-jie.cc/wyy/lrc?rid={rid}'
        
        # 设置请求头
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
        }
        
        # 发送请求
        response = requests.get(api_url, headers=headers, timeout=10)
        response.raise_for_status()
        
        # 返回歌词数据
        return response.text
    except requests.exceptions.RequestException as e:
        return jsonify({'error': f'API请求失败: {str(e)}'}), 500
    except Exception as e:
        return jsonify({'error': f'处理请求时发生错误: {str(e)}'}), 500 